
package projectx.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import jp.gwa.PasswdGet;

import org.apache.log4j.Logger;

import projectx.core.base.ProjectXProperties;
import projectx.core.base.ProjectXSystemException;
import projectx.core.base.ProjectXUnexpectedException;
import projectx.core.base.ResourceUtil;

public class ProjectXDriverManager {

	//--------------------------------------------------------------------------
	//   S T A T I C M E N B E R S
	//
	//--------------------------------------------------------------------------
	private static final String _polarisProductDb = ProjectXProperties
			.getProperty("polaris.product.db", "spadb");
	private static final String _polarisMirrorDb  = ProjectXProperties
	.getProperty("polaris.mirror.db", "spadb");

	private static final Logger _log = Logger
			.getLogger(ProjectXDriverManager.class.getName());

	private static String _userId;
	private static String _passwd;
	
	private static String _mrUserId;
	private static String _mrPasswd;

	private static boolean _initialized;

	//--------------------------------------------------------------------------
	//   S T A T I C M E T H O D S
	//
	//--------------------------------------------------------------------------

	public static void initialize() throws ProjectXSystemException {

		if (!_initialized) {

			synchronized (ProjectXDriverManager.class) {

				if (!_initialized) {

					// initialize JDBC driver
					String jdbcDriver = ProjectXProperties
							.getProperty("jdbc.driver");
					_log.info("Initialize JDBC driver using [" + jdbcDriver
							+ "]...");
					loadClass(jdbcDriver);

					// initialize GWA password(Product DB)
					String passwdFile = ProjectXProperties
							.getProperty("polaris.db.passwd");
					_log.info("Initialize AHE password(Product DB) using [" + passwdFile
							+ "]...");

					PasswdGet passwdGet = PasswdGet.getPasswd(ResourceUtil.loadProperties(passwdFile));
					_userId = passwdGet.getUserid();
					_passwd = passwdGet.getPassword();
					
					// initialize GWA password(Mirror DB)
					String mrPasswdFile = ProjectXProperties.getProperty("polaris.mirror.passwd");
					_log.info("Initialize AHE password(Mirror DB) using [" + mrPasswdFile + "]...");
		
					PasswdGet mrPasswdGet = PasswdGet.getPasswd(ResourceUtil.loadProperties(mrPasswdFile));
					_mrUserId = mrPasswdGet.getUserid();
					_mrPasswd = mrPasswdGet.getPassword();

					_log.info("Initialized successfully");

					_initialized = true;
				}
			}
		}
	}

	private static void loadClass(String className)
			throws ProjectXUnexpectedException {

		try {

			Class.forName(className);
		} catch (ClassNotFoundException e) {
			throw new ProjectXUnexpectedException(e);
		}
	}

	public static boolean isInitialized() {
		return _initialized;
	}

	static Connection getConnection() throws SQLException {

		return DriverManager.getConnection("jdbc:db2:" + _polarisProductDb,
				_userId, _passwd);
	}
	
	static Connection getMrDbConnection() throws SQLException {

		return DriverManager.getConnection("jdbc:db2:" + _polarisMirrorDb,
				_mrUserId, _mrPasswd);
	}
}
